package com.bjyhl.medical.main.video;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.medical.R;
import com.bjyhl.medical.bean.event.MessageEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.yokeyword.eventbusactivityscope.EventBusActivityScope;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class VideoCastFragment extends LatteFragment implements View.OnClickListener {

    public static final String VIDEO_URL = "VIDEO_URL";
    public static final String VIDEO_TITLE = "VIDEO_TITLE";
    private String videoUrl = "";
    private String videoTitle = "";

    private VideoView vv_video = null;
    private TextView tvBack = null;
    private TextView tvTitle = null;

    public static VideoCastFragment create(String videoUrl, String title){
        Bundle bundle = new Bundle();
        bundle.putString(VIDEO_URL, videoUrl);
        bundle.putString(VIDEO_TITLE, title);
        VideoCastFragment fragment = new VideoCastFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        videoUrl = bundle.getString(VIDEO_URL);
        videoTitle = bundle.getString(VIDEO_TITLE);
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_video_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        vv_video = $(R.id.vv_video);
        tvBack = $(R.id.tvBack);
        tvBack.setOnClickListener(this);
        tvTitle = $(R.id.tvTitle);
        tvTitle.setText(videoTitle);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        // 控制视频播放
        MediaController controller = new MediaController(getContext());
        // 建立关联
        vv_video.setMediaController(controller);
        controller.setMediaPlayer(vv_video);
//        // TODO 测试移除
//        videoUrl = "http://jfj8.bjyihuilian.com/new2.mp4";
        vv_video.setVideoURI(Uri.parse(videoUrl));
        vv_video.start();
    }

    @Override
    public void onClick(View v) {
        pop();
    }


    /***************** 以下为接收消息 ********************/
    @Override
    public void onStart() {
        super.onStart();
        // TODO 测试移除
        EventBusActivityScope.getDefault(getActivity()).register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBusActivityScope.getDefault(getActivity()).unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent<String> event){
        tvTitle.setText(event.data);
    }


}
